import logging

import falcon.asgi
import hupper
import uvicorn

import App_setting as setting
from WebBackend.interface.Index import Index
from WebBackend.interface.ThingsAPI import ThingsAPI
from WebBackend.interface.authentication.checking import Checkin
from WebBackend.interface.authentication.login import Login
from WebBackend.interface.management.ConfigFile.ConfigFiles import ConfigFilesAPI
from WebBackend.interface.management.Dashboard.DiskStatus import DiskInfoAPI
from WebBackend.interface.management.Dashboard.ServiceStatus import ServiceStatusAPI
from WebBackend.interface.management.Dashboard.SystemInfo import SystemInfoAPI
from WebBackend.interface.management.Dashboard.SystemStatus import SystemStatusAPI
from WebBackend.interface.management.Directory.Directory import DirectoryAPI
from WebBackend.interface.management.Directory.FileOperation import FileOperationAPI
from WebBackend.interface.management.Directory.SendFiles import SendFileAPI
from WebBackend.interface.management.ExecuteLog import ExecuteLogAPI
from WebBackend.interface.management.ServiceLog import ServiceLogAPI
from WebBackend.interface.management.OptionItems.OptionItems import OptionItemsAPI
from WebBackend.interface.management.ServiceControl import ServiceControlAPI
from WebBackend.interface.management.TaskCenter import TaskCenterAPI
from WebBackend.interface.management.User.User import UserAPI
from WebBackend.interface.management.User.UserTypeStatus import UserTypeStatus
from WebBackend.middleware.VirtualWorldGate_Baihu import VirtualWorldGate_Baihu
from WebBackend.store.ConfigurationStore import ConfigurationStore
from WebBackend.store.TaskStore import TaskStore

from WebBackend.store.UserStore import UserStore
from WebBackend.store.functions.configuration_functions import get_configuration_file_path


def create_app():
    print('creating app')
    true_config_file = get_configuration_file_path()
    cs = ConfigurationStore(true_config_file=true_config_file)
    us = UserStore(cs)
    ts = TaskStore()

    # # # # # # # # # # # # # # # # # #
    sea_archiver = falcon.asgi.App()
    sea_archiver.add_middleware([
        VirtualWorldGate_Baihu()
    ])

    # # # # # # # # # # # # # # # # # #
    sea_archiver.add_route('/', Index())
    sea_archiver.add_sink(
        prefix='^/c12100/(?!.*\.(js|css|html?|jpe?g|png|gif|bmp|ico|svg|woff2?|ttf|eot|pdf|txt)$)[^?#]*$',
        sink=Index.sing_redirect)
    sea_archiver.add_static_route('/', setting.path.dir_html_static_folder)

    # # # # # # # # # # # # # # # # # #

    b = setting.server.base_axios_url
    s = setting.server.base_url
    sea_archiver.add_route(b + '/things', ThingsAPI())

    sea_archiver.add_route(b + '/checkin', Checkin())
    sea_archiver.add_route(b + '/login', Login())
    sea_archiver.add_route(b + '/home/config-file', ConfigFilesAPI(cs))
    sea_archiver.add_route(b + '/home/service-control', ServiceControlAPI(cs))
    sea_archiver.add_route(b + '/home/service-log', ServiceLogAPI(cs))
    sea_archiver.add_route(b + '/home/execute-log', ExecuteLogAPI())
    sea_archiver.add_route(b + '/home/task', TaskCenterAPI(ts))

    sea_archiver.add_route(b + '/home/options', OptionItemsAPI(cs))

    sea_archiver.add_route(b + '/home/directory/list', DirectoryAPI())
    sea_archiver.add_route(b + '/home/directory/file', SendFileAPI())
    sea_archiver.add_route(b + '/home/directory/file-operation', FileOperationAPI(ts))
    sea_archiver.add_route(b + '/home/directory/send-file', SendFileAPI())

    sea_archiver.add_route(b + '/home/user/list', UserAPI(cs))
    sea_archiver.add_route(b + '/home/user/type-control', UserTypeStatus(cs))

    sea_archiver.add_route(b + '/home/dashboard/service-status', ServiceStatusAPI())
    sea_archiver.add_route(b + '/home/dashboard/system-status', SystemStatusAPI())
    sea_archiver.add_route(b + '/home/dashboard/system-info', SystemInfoAPI())
    sea_archiver.add_route(b + '/home/dashboard/disk-info', DiskInfoAPI())

    # # # # # # # # # # # # # # # # # #
    sea_archiver.add_static_route(
        setting.server.base_url,
        setting.path.dir_html_static_folder
    )
    return sea_archiver


# app = create_app()

# logging.basicConfig(level=logging.INFO)


def start_debug_server():
    logging.basicConfig(level=logging.INFO)
    reloader = hupper.start_reloader('App_SeaArchiver.start_debug_server')
    uvicorn.run(create_app(), host=setting.server.host, port=setting.server.port)


if __name__ == "__main__":
    if setting.server.debug:
        start_debug_server()
    else:
        if setting.server.https:
            uvicorn.run("App_SeaArchiver:create_app", workers=1,
                        host=setting.server.host,
                        port=setting.server.port,
                        reload=setting.server.debug,
                        ssl_keyfile=setting.path.file_ssl_key,
                        ssl_certfile=setting.path.file_ssl_cert)
        else:
            uvicorn.run("App_SeaArchiver:create_app", workers=1,
                        host=setting.server.host,
                        port=setting.server.port,
                        reload=setting.server.debug,
                        )
# waitress.serve(app, host='0.0.0.0', port=setting.server.port, threads=8)
